package es.wandererteam.tapakitty;

import org.andengine.entity.scene.background.AutoParallaxBackground;
import org.andengine.entity.scene.background.ParallaxBackground.ParallaxEntity;
import org.andengine.entity.scene.menu.MenuScene;
import org.andengine.entity.scene.menu.MenuScene.IOnMenuItemClickListener;
import org.andengine.entity.scene.menu.item.IMenuItem;
import org.andengine.entity.scene.menu.item.SpriteMenuItem;
import org.andengine.entity.scene.menu.item.decorator.ScaleMenuItemDecorator;
import org.andengine.entity.sprite.Sprite;

import android.content.Intent;
import es.wandererteam.tapakitty.management.BaseScene;
import es.wandererteam.tapakitty.management.SceneManager;
import es.wandererteam.tapakitty.management.SceneManager.SceneType;

public class MainMenuScene extends BaseScene implements
		IOnMenuItemClickListener {
	
	private static final int SELECT_PICTURE = 1;
	final int RETURN_PATH = 33;

	@Override
	public void createScene() {
		createBackground();
		createMenuChildScene();
		resourcesManager.loadMenuAudio();
	//	loadSounds();
	}
	

	public void loadSounds()
	{
	}

	@Override
	public void onBackKeyPressed() {
		System.exit(0);
	}

	@Override
	public SceneType getSceneType() {
		return SceneType.SCENE_MENU;
	}

	@Override
	public void disposeScene() {
		// TODO Auto-generated method stub
	}

	private void createBackground() {
		  AutoParallaxBackground auto = new AutoParallaxBackground(0, 0, 0, 5);
		  auto.attachParallaxEntity(new ParallaxEntity(0, new Sprite(0, 0, resourcesManager.backGroundMenu, vbom)));
		  auto.attachParallaxEntity(new ParallaxEntity(-5.0f, new Sprite(0, 0,  resourcesManager.spikes, vbom))); 
		  auto.attachParallaxEntity(new ParallaxEntity(0, new Sprite(0, 0,  resourcesManager.title, vbom)));
		  this.setBackground(auto); 
		 
	}

	private MenuScene menuChildScene;
	private final int MENU_PLAY = 0;
	private final int MENU_OPTIONS = 1;

	private void createMenuChildScene() {
		menuChildScene = new MenuScene(camera);
		// menuChildScene.setPosition(400, 240);
		menuChildScene.setPosition(0, 0);

		final IMenuItem playMenuItem = new ScaleMenuItemDecorator(
				new SpriteMenuItem(MENU_PLAY, resourcesManager.play_region,
						vbom), 1.2f, 1);
		final IMenuItem optionsMenuItem = new ScaleMenuItemDecorator(
				new SpriteMenuItem(MENU_OPTIONS,
						resourcesManager.options_region, vbom), 1.2f, 1);

		menuChildScene.addMenuItem(playMenuItem);
		menuChildScene.addMenuItem(optionsMenuItem);


		menuChildScene.buildAnimations();
		menuChildScene.setBackgroundEnabled(false);
		/*
		 * playMenuItem.setPosition(playMenuItem.getX(), playMenuItem.getY() +
		 * 10); optionsMenuItem.setPosition(optionsMenuItem.getX(),
		 * optionsMenuItem.getY() - 110);
		 */
		// -----------------------
		// POSICION BOTONES
		// -----------------

		playMenuItem.setPosition(35, 225);
		optionsMenuItem.setPosition(300, 225);
		menuChildScene.setOnMenuItemClickListener(this);
		// IOnMenuItemClickListener

		setChildScene(menuChildScene);
	}

	public boolean onMenuItemClicked(MenuScene pMenuScene, IMenuItem pMenuItem,
			float pMenuItemLocalX, float pMenuItemLocalY) {
		switch (pMenuItem.getID()) {
		case MENU_PLAY:
			resourcesManager.unloadMenuTextures();
			SceneManager.getInstance().loadGameScene(engine);
			
			return true;
		case MENU_OPTIONS:
			Intent intent = new Intent();
			intent.setType("image/*");
			intent.setAction(Intent.ACTION_GET_CONTENT);
			activity.startActivityForResult(Intent.createChooser(intent, "Select Picture or Take Photo in the Gallery to START"),
					SELECT_PICTURE);
			//activity.startActivity(new Intent(activity, TakingPicture.class));
			return true;
		default:
			return false;
		}
	}
	

}
